<?php

namespace common\models\notice;

use common\logics\UserCellLogic;
use mpend\models\User;
use Yii;

/**
 * This is the model class for table "{{%notice_receive}}".
 *
 * @property int $id
 * @property int $user_id 用户ID
 * @property int $notice_id 通告ID
 * @property string $title 消息标题
 * @property string $description 消息描述
 * @property int $from_user_id 发送者ID
 * @property int $actor_id 简历ID
 * @property int $system_notice_id 系统通告ID
 * @property string $type 消息类型
 * @property int $status 状态：0未读，1已读，10已投递
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class NoticeReceive extends \common\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%notice_receive}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['user_id', 'notice_id', 'from_user_id', 'actor_id', 'system_notice_id', 'status', 'created_at', 'updated_at'], 'integer'],
            [['title', 'description'], 'string', 'max' => 255],
            ['type', 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => '用户ID',
            'notice_id' => '通告ID',
            'title' => '消息标题',
            'description' => '消息描述',
            'from_user_id' => '发送者ID',
            'actor_id' => '简历ID',
            'system_notice_id' => '系统通告ID',
            'type' => '消息类型',
            'status' => '状态：0未读，1已读，10已投递',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    public function getNotice()
    {
        return $this->hasOne(Notice::class, ['id' => 'notice_id']);
    }

    public function getSender()
    {
        if ($this->from_user_id) {
            return $this->hasOne(UserCellLogic::class, ['id' => 'from_user_id']);
        } else {
            return [
                'id' => 1,
                'nickname' => '管理员',
                'avatar' => 'http',
                'gender' => '0'
            ];
        }

    }

    public function fields()
    {
        return [
            'id',
            'user_id',
            'title',
            'description',
            'notice',
            'notice_id',
            'status',
            'type',
            'created_at',
            'updated_at',
        ];
    }
}
